package com.example.zhu95.mytimber.listener;

import android.view.View;

import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.utils.Util;

import org.litepal.crud.DataSupport;

import java.util.List;

import jp.co.recruit_lifestyle.android.widget.PlayPauseButton;

import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Pause;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Play;
import static com.example.zhu95.mytimber.activity.BaseActivity.currentPosition;
import static com.example.zhu95.mytimber.activity.BaseActivity.songid;

/**
 * @author zhu95
 * @date 2018/3/16 17:49
 */
public class PlayPauseBtnOnClickListener implements PlayPauseButton.OnControlStatusChangeListener {

    @Override
    public void onStatusChange(View view, boolean state) {
        if (state) {
            int songid = BaseActivity.songid;
            if (songid == -1) {
                try {
                    songid = SongData.songs.get(0).getId();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                songid = songid;
            }
            String songids = Util.INSTANCE.getIdsStrFormat(SongData.songs);
            try {
                List<PlayQueueRecord> playQueueRecords = DataSupport.findAll(PlayQueueRecord.class);
                PlayQueueRecord playQueueRecord = playQueueRecords.get(0);
                String savedSongids = playQueueRecord.getSongids();
                if (savedSongids.contains("" + songids)) {
                    songids = savedSongids;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            MyApplication.sendPlayMusicCmdBroadcast(songid, songids, PlayMusicCmd_Play, currentPosition);
        } else {
            MyApplication.sendPlayMusicCmdBroadcast(songid, "", PlayMusicCmd_Pause);
        }
    }
}
